<template>
  <div class="page">
    <div class="page_top ai-margin-bottom ai-border-bottom">
      <div class="cont ai-margin-tb-sz  ai-margin-lr">
        <div class="cont_left">
          <el-input size="small" v-model="formInline.id" placeholder="请输入工程师ID" style="width: 200px;"
                    class="filter-item"
                    @input="handleContent" @keyup.enter.native="onSubmit"/>
          <el-button size="small" class="filter-item" type="primary" icon="el-icon-search" @click="onSubmit">
            搜索
          </el-button>
          <el-button size="small" class="filter-item" style="margin-left: 5px;" type="primary" icon="el-icon-edit"
                     @click="handleAddEngineer">
            添加教程
          </el-button>
        </div>
        <!--        <div class="cont_right">-->
        <!--          <el-button type="primary" @click="handleAddEngineer">添加维修教程</el-button>-->
        <!--        </div>-->
      </div>
    </div>
    <div class="page_bottom">
      <el-table :data="tableData.slice((currpage - 1) * pageSizes, currpage * pageSizes)" border stripe
                style="width: 100%">
        <el-table-column prop="image" label="封面图片" min-width="120" width="150" align="center">
          <template slot-scope="scope">
            <el-image style="width: 100px;height: 100px" alt="暂无图片" :src="scope.row.photo">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="ID" main-width="100" align="center"></el-table-column>
        <el-table-column prop="title" label="标题" min-width="120" align="center"></el-table-column>
        <!--                <el-table-column prop="content" label="内容" align="center"></el-table-column>-->
        <el-table-column prop="type" label="设备类型" align="center"></el-table-column>
        <el-table-column prop="createTime" label="添加日期" align="center"></el-table-column>
        <el-table-column fixed="right" label="操作" max-width="100" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleEditor(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination ai-margin-tb" style="text-align: center">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage1"
          :page-size="pageSizes"
          :page-sizes="sizes"
          layout="total, prev, pager, next"
          :total="tableData.length"
        ></el-pagination>
        <!-- , prev, pager, next -->
      </div>
    </div>
  </div>
</template>

<script>
  import {deleteProblemById, getProblem, getProblemById} from "../../api/problem";
  import {getEquipmentType} from "../../api/equipment";

  export default {
    name: "maintenanceTutorial",
    data() {
      return {
        loading: true,
        currentPage1: 1,
        currpage: 1,
        pageSizes: 10,
        sizes: [10, 20, 30],
        tableData: [],
        reportType: [
          {
            id: 0,
            value: "巡检",
          }, {
            id: 1,
            value: "调试"
          }, {
            id: 2,
            value: "保养"
          }, {
            id: 3,
            value: "维修"
          }, {
            id: 4,
            value: "培训"
          }, {
            id: 5,
            value: "安装"
          }
        ],
        formInline: {
          id: ""
        },
        equipment_id: "",
        dynamicTags: []
      }
    },
    mounted() {
      this.handleRepairsContent();
      this.handleContent();
    },
    methods: {
      /**
       * 获取维护教程
       */
      handleContent() {
        getProblem()
          .then(rsp => {
            console.log(rsp)
            let table = rsp.result;
            // table.forEach((item, index) => {
            //   let type;
            //   this.dynamicTags.forEach(value => {
            //     if (value.id ===Number(item.type) ){
            //       type = value.type
            //     }
            //   })
            //   item.type = type
            // })
            this.tableData = table
          })
          .catch(respond => {
            console.log(respond.result)

          })
      },
      /**
       * 获取设备类型
       */
      handleRepairsContent() {
        getEquipmentType()
          .then(rsp => {
            console.log(rsp)
            this.dynamicTags = rsp.result;
          })
          .catch(respond => {
          })
      },
      /**
       * 查询
       */
      onSubmit() {
        console.log(this.formInline.id)
        if (this.formInline.id === '') {
          this.$message.warning("请输入维护教程ID")
        } else {
          getProblemById(this.formInline.id)
            .then(rsp => {
              console.log(rsp)
              // this.tableData = []
              this.$message.success(rsp.msg)
              this.tableData = [];
              this.tableData.push(rsp.result)
              // this.tableData = respond.data.result
            })
            .catch(respond => {
              this.$message.warning(respond.msg)
              console.log(respond)
            })
        }
      },
      /**
       * 添加教程
       */
      handleAddEngineer() {
        this.$router.push({
          path: "/MaintenanceTutorial/MarkdownEditor",
          query: {
            state: "1"
          }
        })
      },

      /**
       * 编辑教程
       * @param row
       */
      handleEditor(row) {
        this.$router.push({
          path: "/MaintenanceTutorial/MarkdownEditor",
          query: {
            state: "2",
            data: JSON.stringify(row)
          }
        })
      },//编辑
      handleDelete(row) {
        this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteProblemById(row.id)
            .then(rsp => {
              console.log(rsp)
              this.$message.success(rsp.msg)
              this.handleContent();
            })
            .catch(respond => {
              console.log(respond)

            })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },//删除
      handleSizeChange(val) {
        this.pageSizes = val;
      },
      handleCurrentChange(val) {
        this.currpage = val;
      },
    }
  };
</script>

<style scoped lang="scss">
  .page {
    width: 95%;
    margin: 0 auto;

    .page_top {
      .cont {
        margin: 30px 0px;
        display: flex;
        flex-direction: row;

        .cont_left {
          flex: 1;
        }

        .cont_right {
          flex: 1;
          text-align: right;
        }
      }
    }

    .page_bottom {
      /*position: relative;*/
      /*.pagination{*/
      /*    position: absolute;*/
      /*    bottom: 0;*/
      /*}*/
    }
  }


</style>
